﻿using SoldiHive.Infrustructure.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SoldiHive.Model.Views
{
	[Serializable]
	public class ScheduleView
	{
		public EarningsStatement EarningsStatement { get; set; }
		public IList<Rating> Ratings { get; set; }
		public double AverageRating { get; set; }
		public double AvaragePriceTarget { get; set; }
		public double CurrentPrice { get; set; }

		public ScheduleView(EarningsStatement earningsStatement, double currentPrice, IList<Rating> ratings)
		{
			this.Ratings = ratings;
			this.CurrentPrice = currentPrice;

			this.AverageRating = this.Ratings
				.Where(rating => rating.Rate.HasValue)
				.Select(rating => rating.Rate.Value)
				.Average();

			this.AvaragePriceTarget = this.Ratings
				.Where(rating => rating.PriceTarget.HasValue)
				.Select(rating => rating.PriceTarget.Value)
				.Average();

			this.EarningsStatement = earningsStatement;
		}

		public override string ToString()
		{
			var format = "{0:dd MMM yyy (ddd)} {1} {2:hh:mmtt} {3:Ticker} {4:0.0}/{5} {6} → {7} {8} {9:Name}";

			var scheduleViewString = string.Format(format,
				this.EarningsStatement.LocalStatementDateTime,
				this.EarningsStatement.Time.ToString().Align(6),
				this.EarningsStatement.LocalStatementDateTime,
				this.EarningsStatement.Company,
				this.AverageRating,
				Rating.MaxRate,
				string.Format("${0:0.##}", this.CurrentPrice).Align(7),
				string.Format("${0:0.##}", this.AvaragePriceTarget).Align(7),
				string.Format("({0:0.##}%)", (1 - this.CurrentPrice / this.AvaragePriceTarget) * 100).Align(9),
				this.EarningsStatement.Company.Name);

			return scheduleViewString;
		}
	}
}
